using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using PixelMap;

namespace PixelMap
{
    /// <summary>
    /// The main form of the PixelMapViewer Application.
    /// </summary>
    public partial class MainForm : Form
    {
        private PixelMap pixelMap;
        private ImageFormat imageFormat;

        /// <summary>
        /// The constructor.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.openFileDialog_Main.ShowDialog();
        }

        private void openFileDialog_Main_FileOk(object sender, CancelEventArgs e)
        {
            try
            {
                this.pixelMap = new PixelMap(this.openFileDialog_Main.FileName);
                this.pictureBox_Main.BackgroundImage = this.pixelMap.BitMap;
                this.showToolStripMenuItem.Enabled = true;
                this.greyMapToolStripMenuItem.Enabled = true;
                this.saveToolStripMenuItem.Enabled = true;
            }
            catch (Exception ex)
            {
                ShowException(ex);
            }
        }

        private void ShowException(Exception ex)
        {
            string message = ex.InnerException.Message;
            string caption = "PixelMap Error! " + ex.Message;
            MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void greyMapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                this.pictureBox_Main.BackgroundImage = this.pixelMap.GreyMap;
            }
            catch (Exception ex)
            {
                ShowException(ex);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose(true);
        }

        private void saveFileDialog_Main_FileOk(object sender, CancelEventArgs e)
        {
            try
            {
                this.pictureBox_Main.BackgroundImage.Save(this.saveFileDialog_Main.FileName, this.imageFormat);
            }
            catch (Exception ex)
            {
                ShowException(ex);
            }
        }

        private void bmpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.imageFormat = ImageFormat.Bmp;
            this.saveFileDialog_Main.FileName = "default.bmp";
            this.saveFileDialog_Main.DefaultExt = "bmp";
            this.saveFileDialog_Main.Filter = "Bitmap files|*.bmp";
            this.saveFileDialog_Main.Title = "Saving current image as bitmap...";
            this.saveFileDialog_Main.ShowDialog();
        }

        private void jpgToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.imageFormat = ImageFormat.Jpeg;
            this.saveFileDialog_Main.FileName = "default.jpg";
            this.saveFileDialog_Main.DefaultExt = "jpg";
            this.saveFileDialog_Main.Filter = "JPEG files|*.jpg";
            this.saveFileDialog_Main.Title = "Saving current image as jpeg..."; 
            this.saveFileDialog_Main.ShowDialog();
        }

        private void tiffToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.imageFormat = ImageFormat.Tiff;
            this.saveFileDialog_Main.FileName = "default.tif";
            this.saveFileDialog_Main.DefaultExt = "tif";
            this.saveFileDialog_Main.Filter = "TIFF files|*.tif";
            this.saveFileDialog_Main.Title = "Saving current image as tiff...";
            this.saveFileDialog_Main.ShowDialog();
        }

        private void gifToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.imageFormat = ImageFormat.Gif; 
            this.saveFileDialog_Main.FileName = "default.gif";
            this.saveFileDialog_Main.DefaultExt = "gif";
            this.saveFileDialog_Main.Filter = "GIF files|*.gif";
            this.saveFileDialog_Main.Title = "Saving current image as gif...";
            this.saveFileDialog_Main.ShowDialog();
        }

        private void pngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.imageFormat = ImageFormat.Png;
            this.saveFileDialog_Main.FileName = "default.png";
            this.saveFileDialog_Main.DefaultExt = "png";
            this.saveFileDialog_Main.Filter = "PNG files|*.png";
            this.saveFileDialog_Main.Title = "Saving current image as png...";
            this.saveFileDialog_Main.ShowDialog();
        }
    }
}